package org.jeecg.controller;

import org.jeecg.client.*;
import org.jeecg.client.ws.IWCFService;
import org.jeecg.client.ws.WCFListener;
import org.jeecg.common.api.vo.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description App业务终端
 * @Author wangyi
 * @Date 2022/9/7 15:57
 * @Version 1.0
 **/
@RestController
@RequestMapping("/uapp")
public class UappController {



    public static void main(String[] args) throws Exception {
        // 初始化
        ClientMng mng = new ClientMng();
        // 执行命令
        {
            Map<String, String> parameters = new HashMap<String, String>();
            for(int i = 0; i < 10000; i++)
                parameters.put("param "+ i, i + "");
            BizRequest request = ClientMng.Instance.createRequest("test", "", "", "test", parameters);
            BizResponse response = ClientMng.Instance.RunCmd(request);
            if (!BizResponse.OK.equals(response.getErrorCode()))
                throw new Exception(String.format("Command [%s] failed: %s", request.getCmdName(), response.getErrorCode()));
            String data1 = response.getData("test");
        }
    }

    public void test() throws Exception{
        // 初始化
        ClientMng mng = new ClientMng();

        // 执行命令
        {
            Map<String, String> parameters = new HashMap<String, String>();
            for(int i = 0; i < 10000; i++)
                parameters.put("param "+ i, i + "");
            BizRequest request = ClientMng.Instance.createRequest("test", "", "", "test", parameters);
            BizResponse response = ClientMng.Instance.RunCmd(request);
            if (!BizResponse.OK.equals(response.getErrorCode()))
                throw new Exception(String.format("Command [%s] failed: %s", request.getCmdName(), response.getErrorCode()));
            String data1 = response.getData("test");
        }

        // 执行数据集命令
        {
            Map<String, String> parameters = new HashMap<String, String>();
            parameters.put("sql", "select getdate() date\nselect 1");
            BizRequest request = ClientMng.Instance.createRequest("service", "", "", "querydb", parameters);
            try{
                DataSet response = ClientMng.Instance.RunDbCmd(request);
                Date date = response.getTable(0).getRows().get(0).getDate("date");
                System.out.println(date);
            }catch(Exception ex){
                throw new Exception(String.format("Command [%s] failed: %s", request.getCmdName(), ex.getMessage()));
            }
        }
        // 关闭
        ClientMng.Instance.close();
    }

    @RequestMapping("runDb")
    public Result runDb(String sql){
        try {
            DataSet dataSet = SendUtils.sendRunDbCmd(sql);
            return Result.OK(dataSet.getTables());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping("runCmd")
    public Result runCmd( @RequestBody Map<String,String> map){
        try {
            BizResponse bizResponse = SendUtils.sendRunCmd(map);
            return Result.OK(bizResponse);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping("runCmdDj")
    public Result runCmdDj( @RequestBody Map<String,String> map){
        try {
            BizResponse bizResponse = SendUtils.runCmdDj(map);
            return Result.OK(bizResponse);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
}
